<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkedList">
      <!-- 多选框容器 -->
      <!-- 备选中的值储存在 label 中 -->
      <el-checkbox
        v-for="role in roleList"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-button @click="btnCancel">取消</el-button>
      <el-button type="primary" @click="btnOK">确认</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 全部角色列表
      roleList: [],
      // 选中的角色列表
      checkedList: [],
      userId: ''
    }
  },
  async created() {
    const { rows } = await getRoleListApi({
      page: 1,
      pageSize: 99
    })
    this.roleList = rows
    console.log(44, this.roleList)
  },
  methods: {
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.checkedList = roleIds
      console.log(50, this.checkedList)
      this.userId = id
    },
    async btnOK() {
      await assignRoles({ id: this.userId, roleIds: this.checkedList })
      this.$message.success('修改成功')
      this.$emit('update:showDialog', false)
    },
    btnCancel() {
      this.checkedList = []
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
